/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年8月24日 上午11:33:10
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.woniu.domain.Collect;
import com.woniu.domain.Music;
import com.woniu.domain.MusicType;
import com.woniu.utils.PageBean;
import com.woniu.utils.Queryv;

/**
 * @author Administrator
 *
 */
@Repository
public class MusicDaoImpl implements MusicDao{
	@Autowired
	SessionFactory sf;
	@Override
	public List<Music> findMusicList(PageBean pb, Queryv query) {
		String hql = "from Music m where 1=1";
		if(query!=null) {
			if(query.getSinger() != null && !"".equals(query.getSinger())) {
				hql += " and m.singer like '%"+query.getSinger()+"%'";
			}
			if(query.getTid() != 0) {
				hql += " and m.musicType = "+query.getTid();
			}
		}
		return sf.getCurrentSession().createQuery(hql).setMaxResults(pb.getPageSize()).setFirstResult((pb.getPageNow()-1)*pb.getPageSize()).list();
	}

	@Override
	public int findCount(Queryv query) {
		String hql = "select count(*) from Music m where 1=1";
		if(query!=null) {
			if(query.getSinger() != null && !"".equals(query.getSinger())) {
				hql += " and m.singer like '%"+query.getSinger()+"%'";
			}
			if(query.getTid() != 0) {
				hql += " and m.musicType = "+query.getTid();
			}
		}
		return ((Long)sf.getCurrentSession().createQuery(hql).uniqueResult()).intValue();
	}

	@Override
	public List<MusicType> findAllType() {
		String hql="from MusicType";
		return sf.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public void collectMusic(Collect collect) {
		sf.getCurrentSession().saveOrUpdate(collect);
	}

	@Override
	public List<Collect> findCollectList(Collect collect) {
		String hql = "from Collect c left join Music m on c.music.mid = m.mid where c.user.uid = ?";
		
		return sf.getCurrentSession().createQuery(hql).setInteger(0,collect.getUser().getUid()).list();
	}

}
